package com.magictower.primary.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.magictower.primary.R;
import com.magictower.primary.bean.BlackNumberInfo;
import com.magictower.primary.db.dao.BlackNumberDao;

import java.util.List;

public class BlackCallBaseAdapter extends MyBaseAdapter<BlackNumberInfo> {

    public BlackNumberDao dao = new BlackNumberDao(mContext);

    public BlackCallBaseAdapter(Context context, List<BlackNumberInfo> list) {
        super(context, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_black_call, null);
            holder.tv_number = (TextView) convertView.findViewById(R.id.tv_black_phone);
            holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_black_phone_desc);
            holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
            holder.rl_item_list = (LinearLayout) convertView.findViewById(R.id.rl_item_list);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_number.setText(mList.get(position).getNumber());
        final String mode = mList.get(position).getMode();
        if ("1".equals(mode)) {
            holder.tv_mode.setText("电话拦截");
        } else if ("2".equals(mode)) {
            holder.tv_mode.setText("短信拦截");
        } else if ("3".equals(mode)) {
            holder.tv_mode.setText("电话+短信拦截");
        }
        final BlackNumberInfo info = mList.get(position);
        holder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = info.getNumber();
                Boolean result = dao.delete(number);
                if (result) {
                    Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
                    mList.remove(info);
                    BlackCallBaseAdapter.this.notifyDataSetChanged();
                } else {
                    Toast.makeText(mContext, "删除失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        if (position % 2 == 1) {
            holder.rl_item_list.setBackgroundColor(0x33888888);
        } else {
            holder.rl_item_list.setBackgroundColor(0xFFFFFFFF);
        }
        return convertView;
    }

    static class ViewHolder {
        TextView tv_number;
        TextView tv_mode;
        ImageView iv_delete;
        LinearLayout rl_item_list;
    }
}
